package com.tjl.guess_figure;

import java.util.Random;
import java.util.Scanner;

public class Guess {
    private int randNumber = -1;
    private int guessNumber = -1;
    private Random random = new Random();
    private Scanner scanner = new Scanner(System.in);

    public int getGuessNumber() {
        return guessNumber;
    }

    public void setGuessNumber(int guessNumber) {
        this.guessNumber = guessNumber;
    }


    public synchronized void set() {

        try {
            if (randNumber == guessNumber && randNumber == -1) {
                System.out.println(Thread.currentThread().getName() + " 游戏开始");
                randNumber = random.nextInt(100) + 1;
                System.out.println(randNumber);
                Thread.sleep(100);
                this.notifyAll();
                this.wait();
            } else if (guessNumber != -1) {
                if (guessNumber == randNumber) {
                    System.out.println(Thread.currentThread().getName() + " 恭喜猜对了：");
                    randNumber = -1;
                    guessNumber = -1;

                } else {
                    System.out.println(guessNumber > randNumber ? Thread.currentThread().getName() + " 猜大了，再猜" : Thread.currentThread().getName() + " 猜小了，再猜");
                    guessNumber = -1;
                    Thread.sleep(100);
                    this.notifyAll();
                    this.wait();
                }
            } else {
                this.notifyAll();
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public synchronized void cai() {
        try {
            if (guessNumber == -1) {
                System.out.println(Thread.currentThread().getName() + " 请猜(1-100)：");
                guessNumber = scanner.nextInt();
                Thread.sleep(100);
                this.notifyAll();
                this.wait();
            } else {
                this.notifyAll();
                this.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
